﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Mammoth.Entities;
using Mammoth.Navigations;
using Mammoth.Repositories;
using Mammoth.Views;
using Mammoth.Views.Transaction;
using Mammoth.Events;

namespace Mammoth.ViewModels
{
    public class ValasListViewModel : WPFViewModelBase
    {
        public static readonly RoutedCommand DeleteCommand = new RoutedCommand();
        public static readonly RoutedCommand EditCommand = new RoutedCommand();

        public IList<ValasEntity> ValasList { get; set; }

        IRepository<ValasEntity> repo;

        public ValasListViewModel(IInputElement dispatcher, IRepository<ValasEntity> repo) : base(dispatcher)
        {
            this.repo = repo;
            ReloadDataAsync();
        }

        void ReloadDataAsync()
        {
            Task.Factory.StartNew(ReloadData);
        }

        void ReloadData()
        {
            ValasList = new ObservableCollection<ValasEntity>(repo.Get());
        }

        public void ShowAddValasForm()
        {
            Dispatcher.RaiseNavigationEvent(Events.NavigationType.Show, typeof(AddValasForm));
        }

        public void DeleteHandler(object sender, ExecutedRoutedEventArgs e)
        {
            var par = (ValasEntity)e.Parameter;
            var msg = string.Format("Anda yakin akan menghapus {0}?", par.Name);
            if (Dispatcher.RaiseMessageBoxEvent(msg, "Konfirmasi", 
                MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                repo.Delete(par);
                ReloadDataAsync();
            }
        }

        public void EditHandler(object sender, ExecutedRoutedEventArgs e)
        {
            var par = (ValasEntity)e.Parameter;
            Dispatcher.RaiseNavigationEvent(Events.NavigationType.Show, typeof(AddValasForm),
                new AddValasArguments() { ValasId = par.Id });
        }
    }
}
